import {fromJS} from 'immutable';
import {Utils} from 'fe-lib';
import {asyncHandler, initStatus} from 'shared/utils/reducerUtil';
import * as actionTypes from '../consts';

const {createReducer} = Utils;
const {dataSources, ajaxStatus, formData, formStatus} = actionTypes.key;
const initialState = fromJS({
  [dataSources]: initStatus({}),
  [ajaxStatus]: initStatus({}),
  [formData]: initStatus({}),
  [formStatus]: initStatus({}),
});

const handlers = {
  [actionTypes.GET_DATASOURCE]: asyncHandler(dataSources),
  [actionTypes.AJAX_STATUS]: asyncHandler(ajaxStatus),
  [actionTypes.TEMP_2_REDUX]: asyncHandler(formData),
  [actionTypes.FORM_DATA]: asyncHandler(formData),
  [actionTypes.FORM_STATUS]: asyncHandler(formStatus),
};

export default createReducer(initialState, handlers);
